// Composables
import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'

const routes = [
    {
        path: '/',
        name: "root",
        redirect: "/pc",
    },
    {
        path: '/pc',
        name: "pc",
        component: () => import('@/views/pc-layout.vue'),
        redirect: "/pc/pcHome",
        meta: {title: "Home"},
        children: [
            {
                path: '/pc/pcHome',
                name: 'pcHome',
                // route level code-splitting
                // this generates a separate chunk (Home-[hash].js) for this route
                // which is lazy-loaded when the route is visited.
                component: () => import('@/views/pc/home.vue'),
                meta: {title: 'Home'},
            },
            {
                path: '/pc/category',
                name: 'pcCategory',
                component: () => import('@/views/pc/category.vue'),
                meta: {title: 'Category'}
            },
            {
                path: '/pc/category/:id',
                name: 'pcCategoryDesc',
                component: () => import('@/views/pc/goodsList.vue'),
            },
            {
                path: '/pc/AboutUs',
                name: 'pcAboutUs',
                component: () => import('@/views/pc/AboutUs.vue'),
                meta: {title: 'About us'},
            },
            {
                path: '/pc/AllProducts',
                name: 'pcAllProducts',
                component: () => import('@/views/pc/goodsList.vue'),
                meta: {title: 'All Products'},
            },
            {
                path: '/pc/Recipes',
                name: 'pcRecipes',
                component: () => import('@/views/pc/Recipes.vue'),
                meta: {title: 'Recipes'},
            },
            {
                path: '/pc/Blog',
                name: 'pcBlog',
                component: () => import('@/views/pc/Blog.vue'),
                meta: {title: 'Blog'},
            },
            {
                path: '/pc/FAQ',
                name: 'pcFAQ',
                component: () => import('@/views/pc/FAQ.vue'),
                meta: {title: 'FAQ'},
            },
            {
                path: '/pc/goods/:id',
                name: 'pcGoods',
                component: () => import('@/views/pc/goods.vue'),
            },
            {
                path: '/pc/Account',
                name: 'pcAccount',
                component: () => import('@/views/pc/account.vue'),
                meta: {title: 'My Account'},
            },
            {
                path: '/pc/Login',
                name: 'pcLogin',
                component: () => import('@/views/pc/login.vue'),
                meta: {title: 'Customer Login'},
            },
            {
                path: '/pc/MyCollect',
                name: 'pcMyCollect',
                component: () => import('@/views/pc/myCollect.vue'),
                meta: {title: 'MyFavorite'},
            },
            {
                path: '/pc/Register',
                name: 'pcRegister',
                component: () => import('@/views/pc/register.vue'),
                meta: {title: 'Create an account'},
            },
            {
                path: "/pc/404",
                name: '404',
                component: () => import('@/views/pc/404.vue')
            },
            {
                path: "/:pathMatch(.*)",
                redirect: "/pc/404"
            }
        ],
    },
    {
        path: '/mobile',
        name: "mobile",
        component: () => import('@/views/m-layout.vue'),
        redirect: "/pc/mobile/mobileHome",
        meta: {title: "Home"},
        children: [
            {
                path: 'mobileHome',
                name: 'mobileHome',
                // route level code-splitting
                // this generates a separate chunk (Home-[hash].js) for this route
                // which is lazy-loaded when the route is visited.
                component: () => import('@/views/mobile/home.vue'),
                meta: {title: 'Home'}
            },
            {
                path: 'test',
                name: 'mobileTest',
                component: () => import('@/views/mobile/test.vue'),
                meta: {title: 'test'}
            },
        ],
    },
]


const router = createRouter({
    history: createWebHashHistory(process.env.BASE_URL),
    routes,
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition
        } else {
            return { top: 0 }
        }
    }
})

// 识别手机来源
// export const getTerminalType = () => {
//     if(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i.test(navigator.userAgent)){
//         return 'mobile';
//     }else {
//         return 'pc'
//     }
// }
//
// router.beforeEach(async (to, from, next)=> {
//     let terminalType = await getTerminalType();
//     if (terminalType === 'mobile' && to.fullPath.indexOf('/pc')!==-1){
//         const toPath = to.fullPath.replace("/pc", '/mobile')
//         await next({path: toPath})
//     }else if (terminalType === 'pc' && to.fullPath.indexOf('/mobile')!==-1){
//         const toPath = to.fullPath.replace("/mobile", '/pc')
//         await next({path: toPath})
//     }else {
//         await next()
//     }
// })

export default router
